<?php

$lang_lib = $_LANG['preferences'];
?>
		<script type="text/javascript" language="javascript">
			function save(){
				var xmlhttp;
				var txt = document.getElementById("filter_list").value;
				txt = jQuery.trim(txt);
				alert("Debug: "+txt);
				if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
				  xmlhttp=new XMLHttpRequest();
				}
				else{// code for IE6, IE5
				  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				xmlhttp.onreadystatechange=function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)
					  {
					  	document.getElementById("response").innerHTML=xmlhttp.responseText;
					  }
					}
				xmlhttp.open("GET","save_filterlist.php?text="+escape(txt)+"&name="+document.getElementById("filter_files").options[document.getElementById("filter_files").selectedIndex].value,true);
				xmlhttp.send();
			}
			
			function change_filter()
			{
				var xmlhttpM
				var txt = document.getElementById("filter_files").options[document.getElementById("filter_files").selectedIndex].innerHTML;
				alert(txt);
				if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
				  xmlhttp=new XMLHttpRequest();
				}
				else{// code for IE6, IE5
				  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				xmlhttp.onreadystatechange=function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)
					  {
					  	document.getElementById("filter_list").value=xmlhttp.responseText;
					  }
					}
				xmlhttp.open("GET","change_filterlist.php?q="+txt,true);
				xmlhttp.send();
			}
			
			function change_lang(){
				var sURL = "http://82.130.42.103/test/release2/index.php?p=preferences";
				var xmlhttpM
				var txt = document.getElementById("color_selection").options[document.getElementById("color_selection").selectedIndex].value;
				if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
				  xmlhttp=new XMLHttpRequest();
				}
				else{// code for IE6, IE5
				  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				xmlhttp.onreadystatechange=function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)
					  {
					  	window.location.href = sURL;
					  }
					}
				xmlhttp.open("GET","change_language.php?q="+txt,true);
				xmlhttp.send();
			}
			
		</script>
		
				<div id="settings">
					<h2><?echo $lang_lib['preferences']?></h2>
					<h4><?echo $lang_lib['language']?></h4>
					<select id="color_selection" onchange="change_lang()">
						<?php
						
						/*** 
						
						Creates an array for all supported languages and based on that
						creates dropdown box entries for them. The default choice is determined
						by the language the user has chosen last.
						
						***/
							$languages = array(
								"fi" => "Suomi",
								"en" => "English"
								//TODO Add more languages.
							);
							
							foreach($languages as $key => $value){
								$print_line = '<option value="'.$key.'" ';
								if($_SESSION['language'] == $key){
									$print_line .= 'selected="selected"';
								}
								$print_line .= '> '.$value.' </option>';
								echo $print_line;
							}
						?>
					</select>
					<p id="language_changed"></p>
					<hr/>
					<h4><?echo $lang_lib['filter']?></h4>
					<div id="helpicon"></div>
					<div id="filter_settings">
					
						<select id="filter_files" onchange="change_filter()"><?php 
								$feedback = scandir($_SESSION['filterlist']);
								$i = 2;
								while($feedback[$i]){
									echo '<option value="'.$feedback[$i].'" onchange="change_filter()">'.$feedback[$i].'</option>';
									$i++;
								}
							?></select>
							
						<div id="filterarea">
							<textarea id="filter_list" cols="80" rows="5"><?php 
									include($_SESSION['filterlist'].$_SESSION['filterlist_file']);
								?></textarea>
						</div>
						<a class="small_button" id="submit_button" onclick="save()">Save</a>
						<p id="response"></p>
					</div>
				</div>
